<template>
	<view class="flex_ZC">
		<!-- #ifdef H5 || MP-WEIXIN -->
		<uNavbar title="快捷买单"> </uNavbar>
		<!-- #endif -->
		
		<view class="business flex_dq">
			<image :src="info.cover" mode="aspectFill"></image>
			<view class="business_name flex_Z">
				<text class="f30 c3 fw5">{{info.name}}</text>
				<text class="f23 c6  " style="margin-top: 10rpx;">{{info.pay_master_code.name}}</text>
				<text class="integral">{{info.quick_integral_proportion}}%More值</text>
			</view>
		</view>
		<view class="custom flex_Z">
			<text class="f28 c3">买单金额</text>
			<view class="custom_input flex_dq">
				<text class="symbol">￥</text>
				<input type="digit" v-model.trim="pricess" placeholder="请输入买单金额" />
			</view>
		</view>
		<view class="coupon flex_ld" v-if=" Number(usermoney)>=0.01">
			<view class="coupon_l flex_dq">
				<image :src="baseUrl +'/static/icon/i_dk.png'" mode="aspectFill"></image>
				<text class="f30 c3">使用More券抵扣</text>
			</view>
			<view class="coupon_r flex_dq" @click="changeCoupons">
				<text class="f28 c3">{{usermoney}}</text>
				<image v-if="!isMoney" class="checkboxHead" :src="baseUrl +'/static/icon/i_wxz.png'" mode="aspectFill">
				</image>
				<image v-else class="checkboxHead" :src="baseUrl +'/static/icon/i_xz.png'" mode="aspectFill"></image>
			</view>
		</view>
		<view class="coupon flex_ld" v-if=" Number(usermoney)<0.01">
			<view class="coupon_l flex_dq">
				<image :src="baseUrl +'/static/icon/i_dk.png'" mode="aspectFill"></image>
				<text class="f30 c3">More券余额不足</text>
			</view>
			<view class="coupon_r flex_dq" >
				<text class="f28 c3">{{usermoney}}</text>

			</view>
		</view>
		<view class="btn_Pay flex_c" @click="isPlaceOrder">
			<text class="f36 c0 ">去支付 {{totalPrice}}</text>
		</view>

		<u-popup :show="tipShow" mode="center" @close="tipShow = false" closeable bgColor="transparent">
			<view class="prompt flex_ZC">
				<text class="f36">提示</text>
				<text class="prompt_tit">尚未设置More交易密码？</text>
				<view class="prompt_ntm flex_ld_a">
					<text @click="tipShow = false">取消</text>
					<text @click="goPayPwd">去设置</text>
				</view>
			</view>
		</u-popup>
		<u-popup :show="RealShow" mode="center" @close="RealShow = false" closeable bgColor="transparent">
			<view class="prompt flex_ZC">
				<text class="f36">提示</text>
				<text class="prompt_tit">{{tipstxt}}</text>
				<view class="prompt_ntm flex_ld_a">
					<text @click="RealShow = false">取消</text>
					<text @click="goRealname">去实名</text>
				</view>
			</view>
		</u-popup>
		<u-popup :show="tipShowTwo" mode="center" @close="tipShowTwo = false" closeable bgColor="transparent">
			<view class="prompt flex_ZC">
				<image class="gghimg" :src="baseUrl+  '/static/image/gzhewm.jpg'" mode="widthFix"
					:show-menu-by-longpress="true" @longpress="handleLongPress"></image>
				<text class="prompt_tit">长按图片二维码扫描</text>
				<text class="prompt_tit fw7">请使用微信公众号支付</text>
			</view>
		</u-popup>

		<u-keyboard ref="uKeyboard" dotDisabled mode="number" bgColor="transparent" closeOnClickOverlay :tooltip="false"
			:show="keyboardShow" @close="isClose" @change="changePwd" @backspace="backspace">
			<view class="keyboardss flex_ZC">
				<u-code-input dot space="26" :maxlength="6" v-model="securityPwd"></u-code-input>
			</view>
		</u-keyboard>
	</view>
</template>

<script>
	import {
		myuser
	} from "../../api/user.js"
	import {
		detail,
		goodsdetail,
		createorder
	} from "../../api/local.js"
	export default {
		data() {
			return {
				baseUrl: this.$imageBaseUrl,
				pricess: '',
				id: "",
				info: {},
				isMoney: false,
				payPasword: "",
				keyboardShow: false,
				securityPwd: '',
				tipShow: false,
				secreShow: false, //免密
				tipShowTwo: false,
				user: {

				},
				usermoney: "",
				RealShow:false,
				tipstxt:""
			}
		},
		computed: {
			// 计算总价
			totalPrice() {
				if (this.isMoney) {
					if (Number(this.user.money) - Number(this.pricess) >= 0) {
						return "0.01"
					} else {
						return (Number(this.pricess) - (this.user.money)).toFixed(2);
					}
				} else {
					return Number(this.pricess).toFixed(2);
				}
			},
		},
		watch: {
			securityPwd(newValue) {
				console.log(newValue.length);
				if (newValue.length >= 6) {
					this.keyboardShow = false
					this.isBuyNow()
				}
			}
		},
		onLoad(e) {
			this.id = e.id
			if (!uni.getStorageSync("token")) {
				uni.setStorageSync("quick_pay", `/pagesMy/groundCat/quickCheckout?id=${e.id}`)
			} else {
				uni.removeStorageSync("quick_pay")
			}
			this.userInfo()
			this.getdetail()

		},
		onShow() {
			uni.removeStorageSync("is_pay")
		},
		methods: {
			goRealname(){
				this.RealShow=false
				uni.navigateTo({
					url:"/pageUser/myuser/realname"
				})
			},
			goPayPwd() {
				uni.navigateTo({
					url: "/pageUser/myuser/amendsafepassword"
				})
			},
			isBuyNow() {
				let token = uni.getStorageSync('token');

				if (token) {
					let data = {
						type: '2',
						is_money: this.isMoney ? 1 : 0,
						goods_id: this.id,
						pay_password: this.securityPwd,
						pay_price: this.pricess,
						seller_id: this.id,
						pay_code_id: this.info.pay_code_id,
						number: 1,
						mask: 1
					}
					console.log(data);
					createorder(data).then(res => {
						console.log('详情', res)
						if (res.code == 1) {
							this.payShow = false
							if (res.data.status == 1) {
								uni.navigateTo({
									url: '/pagesMy/cloudCat/paymentCenter?type=2' + '&id=' + res.data.id +
										'&totalPrice=' + this.totalPrice
								})
								return
							}
							uni.$u.toast(res.msg)
							if (this.isMoney) {
								setTimeout(function() {
									uni.redirectTo({
										url: '/pagesMy/cloudCat/paySuccess?type=2'
									})
								}, 1200);
							} else {
								uni.navigateTo({
									url: '/pagesMy/cloudCat/paymentCenter?type=2' + '&id=' + res.data.id +
										'&totalPrice=' + this.totalPrice
								})
							}

						} else  if(res.code==1001) {
							this.tipstxt=res.msg
							this.RealShow=true
							// 清空密码
						}else{
							this.securityPwd = ''
						}
					})
				} else {
					uni.navigateTo({
						url: '/pageLogin/login/login'
					})
				}
			},
			isPlaceOrder() {
				if (!this.pricess) {
					uni.$u.toast("请输入价格")
					return
				}
				if (this.pricess == "0.00") {
					uni.$u.toast("请输入价格")
					return
				}
				// #ifdef H5 || MP-ALIPAY
				if (this.isMoney) {
					this.isBuyNow()
					// if (this.payPasword) {
					// 	if (this.secreShow) {
					// 		this.isBuyNow()
					// 	} else {
					// 		this.keyboardShow = true
					// 		this.payShow = false
					// 	}
					// } else {
					// 	this.payShow = false
					// 	this.tipShow = true
					// }
				} else {
					this.isBuyNow()
				}

				// if (this.payPasword) {
				// 	let money = (this.info.price * this.numbers - this.user.money).toFixed(2);

				// 	if (this.isMoney) {

				// 		if (this.secreShow) {
				// 			this.isBuyNow()
				// 		} else {
				// 			this.keyboardShow = true
				// 			this.payShow = false
				// 		}
				// 	} else {
				// 		this.isBuyNow()
				// 	}
				// } else {
				// 	this.payShow = false
				// 	this.tipShow = true
				// }

				// #endif
				// #ifdef MP-WEIXIN

				this.tipShowTwo = true
				// if (this.payPasword) {
				// 	if (this.isMoney) {
				// 		if (this.secreShow) {
				// 			this.isBuyNow()
				// 		} else {
				// 			this.keyboardShow = true
				// 			this.payShow = false
				// 		}
				// 	} else {
				// 		this.isBuyNow()
				// 	}
				// } else {
				// 	this.payShow = false
				// 	this.tipShow = true
				// }

				// #endif

			},
			isClose() {
				this.keyboardShow = false
				this.payShow = true
			},
			// 输入密码
			changePwd(val) {
				if (this.securityPwd.length < 6) {
					this.securityPwd += val;
					// console.log(this.securityPwd);
				} else {
					console.log('超出不累计');
				}
			},
			// 退格键被点击
			backspace() {
				// 删除value的最后一个字符
				if (this.securityPwd.length) this.securityPwd = this.securityPwd.substr(0, this.securityPwd.length - 1);
				console.log(this.securityPwd);
			},
			userInfo() {
				myuser().then(res => {
					console.log('个人信息', res)
					if (res.code == 1) {
						this.user = res.data
						this.secreShow = res.data.secret_free
						this.usermoney = res.data.money
						this.payPasword = res.data.is_pay_pasword
					}
				})
			},
			changeCoupons() {
				this.isMoney = !this.isMoney
			},
			getdetail() {
				detail({
					id: this.id
				}).then(res => {
					this.info = res.data
					console.log()
				})
			},
			completePay() {
				console.log(11111);
			},
			// 免密支付 
			handleLongPress(e) {
				console.log(e);
			}
		},
	}
</script>

<style>
	page {
		background: #ffffff;
	}
</style>

<style scoped lang="scss">
	.business {
		width: 750rpx;
		padding: 60rpx 60rpx 40rpx;
		/* margin-top: 50rpx; */
		background: #f7f7f7;
	}

	.business>image {
		width: 112rpx;
		height: 112rpx;
		border-radius: 20rpx;
	}

	.business_name {
		margin-left: 20rpx;
	}

	.integral {
		font-size: 24rpx;
		color: #ffffff;
		width: 144rpx;
		height: 32rpx;
		line-height: 32rpx;
		text-align: center;
		background: linear-gradient(90deg, #FA2222 2%, #FFC400 98%);
		border-radius: 38rpx;
		margin-top: 14rpx;
	}

	.custom {
		width: 660rpx;
		margin-top: 62rpx;
	}

	.custom_input {
		/* background: pink; */
		margin-top: 54rpx;
		padding-bottom: 10rpx;
		border-bottom: 1rpx solid #F2F2F2;
	}

	.symbol {
		font-size: 52rpx;
		color: #333333;
		font-weight: 500;
		margin-right: 20rpx;
	}

	.custom_input>input {
		font-size: 36rpx;
		color: #333;
	}

	.btn_Pay {
		width: 690rpx;
		height: 80rpx;
		margin-top: 254rpx;
		background: linear-gradient(90deg, #FA2222 2%, #FFC400 98%);
		border-radius: 138rpx 138rpx 138rpx 138rpx;
	}

	.prompt {
		width: 600rpx;
		background: #ffffff;
		margin: 0 auto;
		border-radius: 20rpx;
		padding-top: 10rpx;
		padding-bottom: 40rpx;
	}

	.prompt>text {
		margin-top: 40rpx;
	}

	.prompt_tit {
		margin: 0 30rpx;
		font-size: 28rpx;
		color: #666666;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
	}

	.prompt_ntm {
		width: 100%;
		font-size: 30rpx;
		margin-top: 50rpx;
	}

	.prompt_ntm>text:nth-child(1) {
		width: 200rpx;
		height: 72rpx;
			display: flex;
				justify-content: center;
				align-items: center;
		border: 1rpx solid #FFC300;
		border-radius: 40rpx;
		color: #FFC300;
	}

	.prompt_ntm>text:nth-child(2) {
		width: 200rpx;
		height: 72rpx;
		line-height: 72rpx;
		text-align: center;
		background: #FFC300;
		border-radius: 40rpx;
		color: #ffffff;
	}

	.coupon {
		width: 690rpx;
		background: #FFFFFF;
		border-radius: 24rpx;
		padding: 24rpx 24rpx;
	}

	.coupon_l>image {
		width: 36rpx;
		height: 36rpx;
		margin-right: 18rpx;
	}

	.checkboxHead {
		width: 36rpx;
		height: 36rpx;
		margin-left: 12rpx;
	}

	.keyboardss {
		width: 750rpx;
		padding: 50rpx 0;
		background: #ffffff;
	}

	.container {
		margin-top: 50rpx;
	}

	.gghimg {
		width: 300rpx;
		height: 300rpx;
		margin-top: 30rpx;
	}
</style>